﻿using System;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;

using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace dcwj
{
    public partial class NewPaper : System.Web.UI.Page
    {
        private String M_CONSTR = ConfigurationManager.ConnectionStrings["dcwjConnectionString"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
                btnNewSave.Attributes.Add("onclick", "return checkSel();");
            }
        }

        private void bind()
        {
            Database objDB = new SqlDatabase(M_CONSTR);
            if (objDB != null)
            {
                //品牌
                DataSet ds = objDB.ExecuteDataSet(CommandType.Text, "SELECT * FROM Table_BRAND");
                ddlBrand.DataSource = ds.Tables[0];
                ddlBrand.DataTextField = "BrandName";
                ddlBrand.DataValueField = "BrandID";
                ddlBrand.DataBind();
                ddlBrand.Items.Insert(0, (new ListItem("--请选择--", "-1")));
                ddlBrand.SelectedIndex = 0;

                //岗位
                ds = objDB.ExecuteDataSet(CommandType.Text, "SELECT * FROM Table_POSITION");
                ddlPosition.DataSource = ds.Tables[0];
                ddlPosition.DataTextField = "PositionName";
                ddlPosition.DataValueField = "PositionID";
                ddlPosition.DataBind();
                ddlPosition.Items.Insert(0, (new ListItem("--请选择--", "-1")));
                ddlPosition.SelectedIndex = 0;

                //区域
                ds = objDB.ExecuteDataSet(CommandType.Text, "SELECT * FROM Table_REGION");
                ddlRegion.DataSource = ds.Tables[0];
                ddlRegion.DataTextField = "RegionName";
                ddlRegion.DataValueField = "RegionID";
                ddlRegion.DataBind();
                ddlRegion.Items.Insert(0, (new ListItem("--请选择--", "-1")));
                ddlRegion.SelectedIndex = 0;

                //所属部门
                ds = objDB.ExecuteDataSet(CommandType.Text, "SELECT * FROM Table_Group");
                ddlTGroup.DataSource = ds.Tables[0];
                ddlTGroup.DataTextField = "GroupName";
                ddlTGroup.DataValueField = "GroupID";
                ddlTGroup.DataBind();
                ddlTGroup.Items.Insert(0, (new ListItem("--全部--", "0")));
                ddlTGroup.SelectedIndex = 0;

                //讲师
                ds = objDB.ExecuteDataSet(CommandType.Text, "SELECT * FROM Table_TEACHER");
                ddlTeacher.DataSource = ds.Tables[0];
                ddlTeacher.DataTextField = "TeacherName";
                ddlTeacher.DataValueField = "TeacherID";
                ddlTeacher.DataBind();
                ddlTeacher.Items.Insert(0, (new ListItem("--请选择--", "-1")));
                ddlTeacher.SelectedIndex = 0;


                //问卷类型
                ds = objDB.ExecuteDataSet(CommandType.Text, "SELECT * FROM Table_PAPERCLASS");
                rdoPaperType.DataSource = ds.Tables[0];
                rdoPaperType.DataTextField = "PaperClassName";
                rdoPaperType.DataValueField = "PaperClassiD";
                rdoPaperType.DataBind();

                //项目经理
                ds = objDB.ExecuteDataSet(CommandType.Text, "SELECT * FROM Table_PM");
                ddlPM.DataSource = ds.Tables[0];
                ddlPM.DataTextField = "PMNM";
                ddlPM.DataValueField = "PMID";
                ddlPM.DataBind();
                ddlPM.Items.Insert(0, (new ListItem("--请选择--", "-1")));
                ddlPM.SelectedIndex = 0;

                //项目类型
                ds = objDB.ExecuteDataSet(CommandType.Text, "SELECT * FROM Table_PTYPE");
                ddlPTYPE.DataSource = ds.Tables[0];
                ddlPTYPE.DataTextField = "PTYPENM";
                ddlPTYPE.DataValueField = "PTYPEID";
                ddlPTYPE.DataBind();
                ddlPTYPE.Items.Insert(0, (new ListItem("--请选择--", "-1")));
                ddlPTYPE.SelectedIndex = 0;

                //培训天数
                for (double d = 0.5; d < 7; d += 0.5)
                {
                    ddlTrainningDays.Items.Add(d.ToString());
                }               
            }
        }

        private void clearUI()
        {
            ddlBrand.SelectedIndex = 0;
            ddlPosition.SelectedIndex = 0;
            ddlRegion.SelectedIndex = 0;
            ddlTeacher.SelectedIndex = 0;
            dtStart.Value = string.Empty;
            dtEnd.Value = string.Empty;
            txtPaperNm.Text = string.Empty;

            ddlPM.SelectedIndex = 0;
            ddlPTYPE.SelectedIndex = 0;
            txtStudentCounts.Text = string.Empty;
            ddlTrainningDays.SelectedIndex = 0;
        }

        protected void ddlTGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlTeacher.Items.Clear();
            Database objDB = new SqlDatabase(M_CONSTR);
            DataSet ds = objDB.ExecuteDataSet(CommandType.Text, "SELECT * FROM Table_TEACHER where groupid=" + ddlTGroup.SelectedValue);
            ddlTeacher.DataSource = ds.Tables[0];
            ddlTeacher.DataTextField = "TeacherName";
            ddlTeacher.DataValueField = "TeacherID";
            ddlTeacher.DataBind();
            ddlTeacher.Items.Insert(0, (new ListItem("--全部--", "0")));
            ddlTeacher.SelectedIndex = 0;
        }
        protected void btnNewSave_Click(object sender, EventArgs e)
        {
            string createTime = DateTime.Now.ToString();
            string pName = txtPaperNm.Text.Trim();
            string teacherID = ddlTeacher.SelectedValue;
            string regionID = ddlRegion.SelectedValue;
            string positionID = ddlPosition.SelectedValue;
            string brandID = ddlBrand.SelectedValue;
            string dtS = dtStart.Value;
            string dtE = dtEnd.Value;
            string pType = rdoPaperType.SelectedValue;

            string PJTTYPE = ddlPTYPE.SelectedValue;
            string pmNM = ddlPM.SelectedValue;
            int totalStu = Int32.Parse(txtStudentCounts.Text.Trim());
            Decimal totalTrainningDays = Decimal.Parse(ddlTrainningDays.SelectedValue);

            if (!string.IsNullOrEmpty(createTime) &&
                !string.IsNullOrEmpty(pName) &&
                !string.IsNullOrEmpty(teacherID) &&
                !string.IsNullOrEmpty(regionID) &&
                !string.IsNullOrEmpty(positionID) &&
                !string.IsNullOrEmpty(brandID) &&
                !string.IsNullOrEmpty(dtS) &&
                !string.IsNullOrEmpty(dtE) &&
                !string.IsNullOrEmpty(pType) &&
                !string.IsNullOrEmpty(pmNM) &&
                !string.IsNullOrEmpty(PJTTYPE) &&
                totalStu>0 && totalTrainningDays>0)
            {
                 Database objDB = new SqlDatabase(M_CONSTR);
                 if (objDB != null)
                 {
                     StringBuilder sb = new StringBuilder();
                     sb.Append(@"INSERT INTO [Table_PAPER]
                                   ([PaperClassID]
                                   ,[PaperName]
                                   ,[TeacherID]
                                   ,[RegionID]
                                   ,[PositionID]
                                   ,[BrandID]
                                   ,[CreateTime]
                                   ,[StartTime]
                                   ,[EndTime]
                                   ,[SubmitTimes]

                                   ,[PMID]
                                   ,[PTYPEID]
                                   ,[TrainingDays]
                                   ,[StudentCounts]
                                        )
                             VALUES
                                   (");
                     sb.Append("'").Append((rdoPaperType.SelectedIndex + 1).ToString()).Append("'").Append(","); ;
                     sb.Append("'").Append(pName).Append("'").Append(",");
                     sb.Append(teacherID).Append(",");
                     sb.Append(regionID).Append(",");
                     sb.Append(positionID).Append(",");
                     sb.Append(brandID).Append(",");
                     sb.Append("'").Append(createTime).Append("'").Append(",");
                     sb.Append("'").Append(dtS).Append("'").Append(",");
                     sb.Append("'").Append(dtE).Append("'").Append(",");
                     sb.Append("0").Append(","); 

                     sb.Append("'").Append(PJTTYPE).Append("'").Append(",");
                     sb.Append("'").Append(pmNM).Append("'").Append(",");
                     sb.Append(totalStu.ToString()).Append(",");
                     sb.Append(totalTrainningDays.ToString()).Append(")");

                     string strSql = sb.ToString().Trim();

                     if (strSql.IndexOf("<") < 0 ||
                         strSql.ToLower().IndexOf("script") < 0 ||
                         strSql.ToLower().IndexOf("cookie") < 0 ||
                         strSql.ToLower().IndexOf("session") < 0)
                     {
                         int n = objDB.ExecuteNonQuery(CommandType.Text, strSql);
                         if (n == 1)
                         {
                             clearUI();
                             Response.Write("<Script Language='JavaScript'>window.alert('" + "新建调查问卷成功，请到问卷查询页面查看详细信息!" + "');</script>");
                         }
                     }                   
                 }
            }
            else
            {
                Response.Write("<Script Language='JavaScript'>window.alert('" + "请输入正确信息!" + "');</script>");
            }
        }
    }
}
